WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
frame_base.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// Fwd refs
22namespace wolf
23{
24class TrajectoryBase;
25class CaptureBase;
26class StateBlock;
27} // namespace wolf
28
29// Wolf includes
30#include "core/common/wolf.h"
33
34namespace wolf
35{
40{
41 friend CaptureBase;
42 friend FactorBase;
43
44 private:
45 TrajectoryBaseWPtr trajectory_ptr_;
46 CaptureBasePtrList capture_list_;
47
48 static unsigned int frame_id_count_;
49
50 protected:
51 unsigned int frame_id_;
53
54 public:
62 FrameBase(const TimeStamp& _ts,
63 const TypeComposite& _frame_types,
64 const VectorComposite& _frame_vectors,
65 const PriorComposite& _frame_priors = {});
66
67 ~FrameBase() override = default;
68
69 // Add and remove from WOLF tree ---------------------------------
72 template <typename classType, typename... T>
73 static std::shared_ptr<classType> emplace(TrajectoryBasePtr _ptr, T&&... all);
74
77 void link(TrajectoryBasePtr);
78
81 void link(ProblemPtr _prb);
82
83 void remove(bool viral_remove_parent_without_children = false) override;
84 bool hasChildren() const override;
85
86 // Frame properties -----------------------------------------------
87 public:
88 unsigned int id() const override;
89
90 // Frame values ------------------------------------------------
91 public:
94 void setTimeStamp(const TimeStamp& _ts);
95
98 TimeStamp getTimeStamp() const;
99
102 void getTimeStamp(TimeStamp& _ts) const;
103
104 // State blocks ----------------------------------------------------
105 public:
108 StateBlockConstPtr getV() const;
109
112 StateBlockPtr getV();
113
114 protected:
115 void setProblem(ProblemPtr _problem) override final;
116
117 // Wolf tree access ---------------------------------------------------
118 public:
121 TrajectoryBaseConstPtr getTrajectory() const;
122
125 TrajectoryBasePtr getTrajectory();
126
129 FrameBaseConstPtr getPreviousFrame(const unsigned int& i = 1) const;
130
133 FrameBaseConstPtr getNextFrame(const unsigned int& i = 1) const;
134
137 FrameBasePtr getPreviousFrame(const unsigned int& i = 1);
138
141 FrameBasePtr getNextFrame(const unsigned int& i = 1);
142
145 CaptureBaseConstPtrList getCaptureList() const;
146
149 CaptureBasePtrList getCaptureList();
150
153 bool hasCapture(const CaptureBaseConstPtr& _capture) const;
154
157 FactorBaseConstPtrList getFactorList() const;
158
161 FactorBasePtrList getFactorList();
162
165 void getFactorList(FactorBaseConstPtrList& _fac_list) const;
166
169 void getFactorList(FactorBasePtrList& _fac_list);
170
173 template <class C>
174 std::shared_ptr<const C> getCaptureOfType() const;
175
178 template <class C>
179 std::shared_ptr<C> getCaptureOfType();
180
183 CaptureBaseConstPtr getCaptureOfType(const std::string& type) const;
184
187 CaptureBasePtr getCaptureOfType(const std::string& type);
188
191 template <class C>
192 std::list<std::shared_ptr<const C>> getCapturesOfType() const;
193
196 template <class C>
197 std::list<std::shared_ptr<C>> getCapturesOfType();
198
201 CaptureBaseConstPtrList getCapturesOfType(const std::string& type) const;
202
205 CaptureBasePtrList getCapturesOfType(const std::string& type);
206
209 CaptureBaseConstPtr getCaptureOf(SensorBaseConstPtr _sensor_ptr) const;
210
213 CaptureBasePtr getCaptureOf(SensorBaseConstPtr _sensor_ptr);
214
217 CaptureBaseConstPtr getCaptureOf(SensorBaseConstPtr _sensor_ptr, const std::string& type) const;
218
221 CaptureBasePtr getCaptureOf(SensorBasePtr _sensor_ptr, const std::string& type);
222
225 CaptureBaseConstPtrList getCapturesOf(SensorBaseConstPtr _sensor_ptr) const;
226
229 CaptureBasePtrList getCapturesOf(SensorBasePtr _sensor_ptr);
230
231 // Debug and info -------------------------------------------------------
234 virtual void printHeader(int depth, //
235 bool factored_by, //
236 bool metric, //
237 bool state_blocks,
238 std::ostream& stream,
239 std::string _tabs = "") const;
240
243 void print(int depth, //
244 bool factored_by, //
245 bool metric, //
246 bool state_blocks,
247 std::ostream& stream = std::cout,
248 std::string _tabs = "") const;
249
252 virtual CheckLog localCheck(bool _verbose, std::ostream& _stream, std::string _tabs = "") const;
253
256 bool check(CheckLog& _log, bool _verbose, std::ostream& _stream, std::string _tabs = "") const;
257
258 private:
261 CaptureBasePtr addCapture(CaptureBasePtr _capt_ptr);
262
265 void removeCapture(CaptureBasePtr _capt_ptr);
266
269 void setTrajectory(TrajectoryBasePtr _trj_ptr);
270
271 public:
275 {
276 return std::static_pointer_cast<FrameBase>(shared_from_this());
277 };
278
281 FrameBaseConstPtr shared_from_this_frame() const
282 {
283 return std::static_pointer_cast<const FrameBase>(shared_from_this());
284 };
285};
286
287} // namespace wolf
288
289// IMPLEMENTATION //
290
295
296namespace wolf
297{
298template <typename classType, typename... T>
299std::shared_ptr<classType> FrameBase::emplace(TrajectoryBasePtr _ptr, T&&... all)
300{
301 std::shared_ptr<classType> frm = std::make_shared<classType>(std::forward<T>(all)...);
302 frm->emplacePriors();
303 frm->link(_ptr);
304 return frm;
305}
306
307inline unsigned int FrameBase::id() const
308{
309 return frame_id_;
310}
311
312inline void FrameBase::getTimeStamp(TimeStamp& _ts) const
313{
314 _ts = time_stamp_;
315}
316
318{
319 return time_stamp_;
320}
321
322inline StateBlockConstPtr FrameBase::getV() const
323{
324 return getStateBlock('V');
325}
326
327inline StateBlockPtr FrameBase::getV()
328{
329 return getStateBlock('V');
330}
331
332inline TrajectoryBaseConstPtr FrameBase::getTrajectory() const
333{
334 return trajectory_ptr_.lock();
335}
336
337inline TrajectoryBasePtr FrameBase::getTrajectory()
338{
339 return trajectory_ptr_.lock();
340}
341
342inline CaptureBaseConstPtrList FrameBase::getCaptureList() const
343{
344 CaptureBaseConstPtrList list_const;
345 for (auto&& obj_ptr : capture_list_) list_const.push_back(obj_ptr);
346 return list_const;
347}
348
349inline CaptureBasePtrList FrameBase::getCaptureList()
350{
351 return capture_list_;
352}
353
354inline void FrameBase::setTrajectory(TrajectoryBasePtr _trj_ptr)
355{
356 trajectory_ptr_ = _trj_ptr;
357}
358
359template <class C>
360inline std::shared_ptr<const C> FrameBase::getCaptureOfType() const
361{
362 for (auto capture_ptr : getCaptureList())
363 {
364 auto cap_derived = std::dynamic_pointer_cast<const C>(capture_ptr);
365 if (cap_derived) return cap_derived;
366 }
367 return nullptr;
368}
369
370template <class C>
371inline std::shared_ptr<C> FrameBase::getCaptureOfType()
372{
373 for (auto capture_ptr : getCaptureList())
374 {
375 auto cap_derived = std::dynamic_pointer_cast<C>(capture_ptr);
376 if (cap_derived) return cap_derived;
377 }
378 return nullptr;
379}
380
381template <class C>
382inline std::list<std::shared_ptr<const C>> FrameBase::getCapturesOfType() const
383{
384 std::list<std::shared_ptr<const C>> captures;
385 for (auto capture_ptr : getCaptureList())
386 {
387 auto cap_derived = std::dynamic_pointer_cast<const C>(capture_ptr);
388 if (cap_derived) captures.push_back(cap_derived);
389 }
390 return captures;
391}
392
393template <class C>
394inline std::list<std::shared_ptr<C>> FrameBase::getCapturesOfType()
395{
396 std::list<std::shared_ptr<C>> captures;
397 for (auto capture_ptr : getCaptureList())
398 {
399 auto cap_derived = std::dynamic_pointer_cast<C>(capture_ptr);
400 if (cap_derived) captures.push_back(cap_derived);
401 }
402 return captures;
403}
404
405} // namespace wolf
Base class of captures: a sensor measurement taken at a time stamp, linked to a frame,...
Definition: capture_base.h:41
Log of a consistency check: whether it is consistent and the explanation of the inconsistencies.
Definition: check_log.h:30
Base class of factors: a constraint between state blocks of one or more nodes, given a measurement an...
Definition: factor_base.h:73
Base class of frames: the states of the robot at a time stamp, in the trajectory, with the captures t...
Definition: frame_base.h:40
virtual void printHeader(int depth, bool factored_by, bool metric, bool state_blocks, std::ostream &stream, std::string _tabs="") const
Prints the information of this node (not of its children)
Definition: frame_base.cpp:267
CaptureBaseConstPtrList getCapturesOf(SensorBaseConstPtr _sensor_ptr) const
Returns the captures of a sensor.
Definition: frame_base.cpp:194
virtual CheckLog localCheck(bool _verbose, std::ostream &_stream, std::string _tabs="") const
Checks the consistency of this node (not of its children)
Definition: frame_base.cpp:300
~FrameBase() override=default
bool hasCapture(const CaptureBaseConstPtr &_capture) const
Returns whether the capture belongs to the frame.
Definition: frame_base.cpp:236
TimeStamp getTimeStamp() const
Returns the time stamp.
Definition: frame_base.h:317
unsigned int id() const override
Returns the id of the node, unique among the nodes of its category.
Definition: frame_base.h:307
FactorBaseConstPtrList getFactorList() const
Returns the factors of all the captures of the frame.
Definition: frame_base.cpp:210
void setTimeStamp(const TimeStamp &_ts)
Sets the time stamp.
Definition: frame_base.cpp:78
FrameBaseConstPtr getPreviousFrame(const unsigned int &i=1) const
Returns the i-th previous frame in the trajectory (nullptr if none)
Definition: frame_base.cpp:90
unsigned int frame_id_
Frame id, unique among frames.
Definition: frame_base.h:51
FrameBaseConstPtr shared_from_this_frame() const
Returns a shared pointer to this frame.
Definition: frame_base.h:281
bool check(CheckLog &_log, bool _verbose, std::ostream &_stream, std::string _tabs="") const
Checks the consistency of this node and its children, composing the log.
Definition: frame_base.cpp:342
std::list< std::shared_ptr< const C > > getCapturesOfType() const
Returns the captures of class C.
Definition: frame_base.h:382
TimeStamp time_stamp_
Frame time stamp.
Definition: frame_base.h:52
CaptureBaseConstPtrList getCaptureList() const
Returns the captures of the frame.
Definition: frame_base.h:342
std::shared_ptr< const C > getCaptureOfType() const
Returns the first capture of class C (nullptr if none)
Definition: frame_base.h:360
TrajectoryBaseConstPtr getTrajectory() const
Returns the trajectory the frame belongs to.
Definition: frame_base.h:332
void link(TrajectoryBasePtr)
Links the frame to a trajectory.
Definition: frame_base.cpp:241
void remove(bool viral_remove_parent_without_children=false) override
Removes the node: its state blocks and the factors constraining them.
Definition: frame_base.cpp:44
FrameBasePtr shared_from_this_frame()
Returns a shared pointer to this frame.
Definition: frame_base.h:274
void print(int depth, bool factored_by, bool metric, bool state_blocks, std::ostream &stream=std::cout, std::string _tabs="") const
Prints this node and, depending on the depth, its children.
Definition: frame_base.cpp:287
FrameBase(const TimeStamp &_ts, const TypeComposite &_frame_types, const VectorComposite &_frame_vectors, const PriorComposite &_frame_priors={})
Constructor from a time stamp and the state specs (types, vectors, priors)
Definition: frame_base.cpp:32
StateBlockConstPtr getV() const
Returns the state block of key 'V' (nullptr if none)
Definition: frame_base.h:322
FrameBaseConstPtr getNextFrame(const unsigned int &i=1) const
Returns the i-th next frame in the trajectory (nullptr if none)
Definition: frame_base.cpp:104
void setProblem(ProblemPtr _problem) override final
Sets the problem (if not set yet) and notifies it of all the state blocks and prior factors.
Definition: frame_base.cpp:258
CaptureBaseConstPtr getCaptureOf(SensorBaseConstPtr _sensor_ptr) const
Returns the first capture of a sensor (nullptr if none)
Definition: frame_base.cpp:178
static std::shared_ptr< classType > emplace(TrajectoryBasePtr _ptr, T &&... all)
Creates a frame of class classType, emplaces its priors and links it to the trajectory.
Definition: frame_base.h:299
bool hasChildren() const override
Returns whether the node has children in the Wolf tree.
Definition: frame_base.cpp:73
Base class of the nodes having state blocks (sensors, frames, captures and landmarks)
Definition: node_state_blocks.h:36
virtual StateBlockConstPtr getStateBlock(const char &_sb_key) const
Returns the state block of a key (nullptr if none)
Definition: node_state_blocks.h:433
TimeStamp implements basic functionalities for time stamps.
Definition: time_stamp.h:39
Composite of state vectors.
Definition: vector_composite.h:29
Definition: capture_base.h:23